var typeCode = '';
var typeName;
var upload;
layui.use(['upload'], function () {
    upload = layui.upload;
    upload.render({
        elem: '#import',   //触发选择器
        url: '/tu/sys/dictVal/uploadFile',   //上传路劲
        accept: 'file', //普通文件
        data: {
            typeCode: function () {
                return $("#typeCode").val();
            }
        },
        method: "post",
        done: function (res, index, upload) {    //返回函数
            if (res.code == '99999') {
                layuiFunciton.warn(res.msg, function () {
                    layuiFunciton.refresh('', 'layerFormIds');
                })
            } else {
                layuiFunciton.success('操作成功!', function () {
                    layuiFunciton.refresh('', 'layerFormIds');
                })
            }
        }
    });
});
layuiFunciton.tableInit({
    toolbar: '.btn',
    tableId: 'newsList',
    url: '/tu/sys/dictType/listAll',
    id: 'layerFormId',
    searchFromId: '',
    height: 'full-200',
    tableFilter: 'newsLists',  //表格监听名称,
    page: false,
    cols: [[
        {
            field: 'typeName',
            title: '字典类型名称',
            style: 'cursor: pointer;',
            align: 'center',
            sort: true
        }, {
            field: 'typeCode',
            title: '字典类型编码',
            style: 'cursor: pointer;',
            align: 'center',
            sort: true
        }, {
            title: '操作',
            width: 170,
            templet: '#dictListBar',
            fixed: "right",
            align: "center"

        }
    ]],
    tools: {
        newsLists: function (rowData) {
            typeCode = rowData.typeCode;
            typeName = rowData.typeName;
            $(".addNews_btn2").html("新增【" + typeName + "】字典值");
            loadata(typeCode)
            $("#typeCode").val(typeCode);
        },
        edit: function (rowData) {
            layuiFunciton.layerOpen({
                id: "123",
                title: "修改内容",
                areaType: true,
                content: "edit.html",
                finder: 'form',
                data: rowData,
                end: function () {
                    layuiFunciton.refresh('', 'layerFormId');
                }
            });
        },
        del: function (rowData) {
            layuiFunciton.alert({
                types: 'confirm', title: '确定删除此条数据？', success: function () {
                    layuiFunciton.ajaxRequest({
                        url: layuiFunciton.URL + '/sys/dictType/deleteEntity',
                        method: 'get',
                        data: {id: rowData.id},
                        success: function (data) {
                            layuiFunciton.refresh('', 'layerFormId');
                        }
                    });
                }
            });
        },
        addNews_btn: function (rowData) {//新增字典类型
            layuiFunciton.layerOpen({
                title: "新增字典类型",
                content: "dictAdd.html",
                areaType: true,
                finder: 'form',
                data: {menuLevel: (1 + $("#menuLevel").val())},
                end: function () {
                    layuiFunciton.refresh('', 'layerFormId');
                }
            });
        }
    }
})
loadata(typeCode)
function loadata(v_typeCode) {
    var colsList = [[
        {
            field: 'valName', title: '字典值名称', align: "center",
            sort: true
        },
        {
            field: 'valCode', title: '字典值编码', align: "center",
            sort: true
        },

        {
            field: 'enabled', title: '是否启用', width: 110, sort: true, templet: function (data) {
            var html = '<input type="checkbox" name="enabled" value="' + data.id + '" lay-skin="switch" lay-text="启用|禁用" lay-filter="enabledDemo" ';
            if (data.enabled == 1) {
                html += 'checked';
            }
            html += '>';
            return html;
        }
        }
    ]];
    if (v_typeCode && 'JSR' == v_typeCode) {
        colsList[0].push(
            {
                field: '', title: '类型', width: 110, sort: true,
                templet: function (data) {
                    if ('1' == data.description) {
                        return "个人";
                    } else if ('2' == data.description) {
                        return "单位";
                    }
                }
            });
    }
    colsList[0].push({title: '操作', width: 168, templet: '#dictEDIT', fixed: "right", align: "center"});
    layuiFunciton.tableInit({
        toolbar: '.btns',
        tableId: 'dictValList',
        url: '/tu/sys/dictVal/getByTypeCode?typeCode=' + v_typeCode,
        id: 'layerFormIds',
        height: 'full-200',
        tableFilter: 'newsList2',  //表格监听名称,
        page: false,
        cols: colsList,
        returnData: function () {
            layuiFunciton.uploadFile({
                elem: '.import',   //触发选择器
                url: '/tu/sys/dictVal/uploadFile?typeCode=' + typeCode,   //上传路劲
                accept: 'file', //普通文件
                done: function (res, index, upload) {    //返回函数
                    console.log(res, index, upload)
                }
            });
        },
        tools: {
            //导入
            import: function (rowData) {
                if (typeName) {
                    $("#import").click();
                } else {
                    layuiFunciton.warn('请选择字典类型');
                }
            }
            ,
            //下载模板
            download: function (rowData) {
                window.location.href = '字典值模板.xlsx';
            }
            ,
            edit1: function (rowData) {
                rowData.typeCode = typeCode;
                layuiFunciton.layerOpen({
                    title: "修改内容",
                    content: "editVal.html",
                    areaType: true,
                    finder: 'form',
                    data: rowData,
                    end: function () {
                        layuiFunciton.refresh('', 'layerFormIds');
                    }
                });
            }
            ,
            del1: function (rowData) {
                layuiFunciton.alert({
                    types: 'confirm', title: '确定删除此条数据？', success: function () {
                        layuiFunciton.ajaxRequest({
                            url: layuiFunciton.URL + '/sys/dictVal/deleteEntity',
                            method: 'get',
                            data: {id: rowData.id},
                            success: function (data) {
                                layuiFunciton.refresh('', 'layerFormIds');
                            }
                        });
                    }
                });
            }
            ,
            addNews_btn2: function (rowData) {//新增字典值
                if (typeName) {
                    layuiFunciton.layerOpen({
                        title: "新增【" + typeName + "】字典值",
                        content: "dictValAdd.html",
                        areaType: true,
                        finder: 'form',
                        data: {menuLevel: (1 + $("#menuLevel").val()), typeCode: typeCode},
                        end: function () {
                            layuiFunciton.refresh('', 'layerFormIds');
                        }
                    });
                } else {
                    layuiFunciton.warn('请选择字典类型');
                }
            }
        }
        ,
        filter: [
            {
                types: 'form',   //   注：仅支持 from 、table 两种类型  监听 table的 tools 请使用 tools 属性
                filterTypes: 'switch',   //
                filterName: 'enabledDemo',
                returnData: function (obj) {
                    layuiFunciton.ajaxRequest({
                        url: layuiFunciton.URL + '/sys/dictVal/editEntity',
                        method: 'post',
                        data: {id: obj.value, enabled: obj.elem.checked ? 1 : 0},
                        success: function (data) {
                            layuiFunciton.refresh('', 'layerFormIds');
                        }
                    });
                }
            }
        ]
    })
}



